package com.leyou.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.page.PageResult;
import com.leyou.entity.CategoryBrandEntity;
import com.leyou.service.CategoryBrandService;
import com.leyou.vo.BrandSaveParamVo;
import com.leyou.vo.BreadPageParamVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.dao.BrandDao;
import com.leyou.entity.BrandEntity;
import com.leyou.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
@Transactional  //开启事务
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public void deleteById(Long id) {
        this.removeById(id);
        categoryBrandService.deleteBybreadId(id);
    }

    /**
     * 根据分类id来查询品牌信息
     * @param cid
     * @return
     */
    @Override
    public List<BrandEntity> getBrandByCategoyId(Long cid) {
        List<CategoryBrandEntity>  list =  categoryBrandService.getInfoByCategoyId(cid);
        List<Long> breadids = list.stream().map((item) -> {
            return item.getBrandId();
        }).collect(Collectors.toList());

        List<BrandEntity> brandList = this.list(
                new QueryWrapper<BrandEntity>().in("id", breadids)
        );

        return brandList;
    }

    /**
     * 修改品牌信息  并修改分类&品牌关系表
     * @param brand
     */
    @Override
    public void updateBatchCategory(BrandSaveParamVo brand) {
        BrandEntity brandEntity = new BrandEntity();
        BeanUtils.copyProperties(brand,brandEntity);
        //  修改品牌信息
        this.updateById(brandEntity);

        // 先删除关于该品牌的信息 再重新插入
        categoryBrandService.deleteBybreadId(brandEntity.getId());

        List<CategoryBrandEntity> collect = brand.getCids().stream().map(item -> {
            CategoryBrandEntity categoryBrandEntity = new CategoryBrandEntity();
            categoryBrandEntity.setBrandId(brandEntity.getId());
            categoryBrandEntity.setCategoryId(item);
            return categoryBrandEntity;
        }).collect(Collectors.toList());

        categoryBrandService.saveBatch(collect);
    }

    /**
     * 保存品牌信息  并保存分类&品牌关系表
     * @param brand
     */
    @Override
    public void SaveBatchCategory(BrandSaveParamVo brand) {
        BrandEntity brandEntity = new BrandEntity();
        BeanUtils.copyProperties(brand,brandEntity);
        this.save(brandEntity);
        List<CategoryBrandEntity> collect = brand.getCids().stream().map(item -> {
            CategoryBrandEntity categoryBrandEntity = new CategoryBrandEntity();
            categoryBrandEntity.setBrandId(brandEntity.getId());
            categoryBrandEntity.setCategoryId(item);
            return categoryBrandEntity;
        }).collect(Collectors.toList());
        // 批量存分类品来关系表
        categoryBrandService.saveBatch(collect);

    }

    /**
     * 根据查询条件分页并排序查询品牌信息
     * @param paramVo
     * @return
     */
    @Override
    public PageResult<BrandEntity> getBrandPages(BreadPageParamVo paramVo) {

        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(paramVo.getKey())){
            wrapper.like("name",paramVo.getKey()).or().eq("letter",paramVo.getKey());
        }
       // wrapper.
        if(!StringUtils.isEmpty(paramVo.getSortBy())){
           if(paramVo.getDesc()){
               wrapper.orderByDesc(paramVo.getSortBy());
           }else {
               wrapper.orderByAsc(paramVo.getSortBy());
           }
        }
        //参数一是当前页，参数二是每页个数
        IPage<BrandEntity> userPage = new Page<>(paramVo.getPage(), paramVo.getRows());
        IPage<BrandEntity> iPage = baseMapper.selectPage(userPage, wrapper);
        List<BrandEntity> brandpages = iPage.getRecords();
        return new PageResult<>(iPage.getTotal(),brandpages);
    }
}